<?php
namespace User\Lib;

/**
 * @author [HFei] 2016-12-29
 */
class ImiLib {
	/**
	 * [userInfo 单用户信息]
	 * @author Hfei 2016-12-29
	 * @param  [number] $user_id [用户id]
	 * @return [array]          [用户信息]
	 */
	// 查询用户以及三方登录信息
	public static function getUserById($user_id) {
//		$userModel = D('User/UserAddition');
//		$db_prefix=C('DB_PREFIX');
//		$userInformation = $userModel
//				->join('RIGHT JOIN __USERS__ ON __USER_ADDITION__.user_id = __USERS__.id')
//				->where($db_prefix."users.id = $user_id")
//				->find();
		/*EDIT 改为单表查询 2017-2-21*/
		$userModel = D('User/Users');
		$map['id'] = $user_id;
		$userInformation = $userModel->where($map)->find();
		$userInformation['avatar'] = sp_get_user_avatar_url($userInformation['avatar']);
		return $userInformation;
	}
	/**
	 * [userGroupInfo 查询用户组信息]
	 * @author Hfei 2016-12-29
	 * @param  [array] $userGroupId [用户组id]
	 * @return [array]              [用户组信息]
	 */
	public static function getUserByIds($userGroupId) {
//		$model = D('User/UserAddition');
//		$userGroupInfomation = $model
//					->join('RIGHT JOIN __USERS__ ON __USER_ADDITION__.user_id = __USERS__.id')
//					->where($map)
//					->select();
		/*EDIT 改为单表查询 2017-2-21*/
		$model = D('User/Users');
		$map['id'] = array('in',$userGroupId);
		$userGroupInfomation = $model->where($map)->select();
		foreach ($userGroupInfomation as $key => $value) {
			$userGroupInfomation[$key]['avatar'] = sp_get_user_avatar_url($userGroupInfomation[$key]['avatar']);
		}
		return $userGroupInfomation;
	}
	/**
	 * [getAvatarByUserId 根据用户ID获取用户头像]
	 * @param  [type] $user_id [用户id]
	 * @return [type]          [用户头像和用户id]
	 */
	public static function getAvatarByUserId($user_id) {
		$userModel = D("Common/Users");
		$userAvatar = $userModel->field('avatar')->find($user_id);
		$userInfo['id'] = $user_id;
		$userInfo['avatar'] = sp_get_user_avatar_url($userAvatar['avatar']);
		return $userInfo;
	}
	/**
	 * [getAvatarByUserIds 根据用户ID组成的数组获取用户头像列表]
	 * @param  [type] $userGroupId [用户组id]
	 * @return [type]              [用户组头像信息和id]
	 */
	public static function getAvatarByUserIds($userGroupId) {
		$userModel = D("Common/Users");
		$map['id'] = array('in',$userGroupId);
		$userGroupAvatar = $userModel->where($map)->field('id,avatar')->select();
		foreach ($userGroupAvatar as $key => $value) {
			$userGroupAvatar[$key]['avatar'] = sp_get_user_avatar_url($userGroupAvatar[$key]['avatar']);
		}
		return $userGroupAvatar;
	}

	/**
	 * [getCurrentUser 获取当前用户信息]
	 * @return [type] [description]
	 */
	public static function getCurrentUser(){
		$user = session('user');
		if (!empty($user) && isset($user['id'])){
			return $user;
//			$user_id = $user['id'];
//			$userModel = D('User/UserAddition');
//			$db_prefix=C('DB_PREFIX');
//			$userInformation = $userModel
//				->join('RIGHT JOIN __USERS__ ON __USER_ADDITION__.user_id = __USERS__.id')
//				->where($db_prefix."users.id = $user_id")
//				->find();
//			return $userInformation;
		} else {
			return FALSE;
		}
	}
	/**
	 * [getCurrentUserId 当前用户id]
	 * @return [type] [description]
	 */
	public static function getCurrentUserId() {
		$user = session('user');
		$user_id = $user['id'];
		return $user_id;
	}

	/**
	 * 登录检测,强制要求登录时调用此方法
	 * @author	Baiyu	2017-01-16
	 */
	public static function checkLogin($that = FALSE){
		$user = self::getCurrentUser();
		if (empty($user)) {
			if (IS_APP || IS_AJAX) {
				$that && $that -> output(array(
						'error' => 201,
						'error_msg' => '请先登录',
						'data' => array()
				), 'json');
			} else {
				$that && $that -> redirect('/');		//跳到登录地址
			}
			exit('login required');
		}
	}

	/**
	 * @param $userMobile   用户手机号
	 * 通过手机号获取用户信息
	 */
	public static function getUserInfoByUserMobile($userMobile){
		$userModel = D("Users");
		$userinfo = $userModel
			->where(array('mobile'=>$userMobile))
			->find();
		return $userinfo;
	}

	/**
	 * 更新用户数据
	 * @param $userID 用户ID
	 * @param $filedName    字段名称
	 * @param $information 数据
	 */
	public static function updataInfoByFieldName($userID,$filedName,$information){
		$userModel = D("Users");
		$data[$filedName] = $information;
		$result = $userModel
			->where(array('id'=>$userID))
			->data($data)
			->save();
		return $result;
	}
}
